/*
 * 版权所有(C)，上海海鼎信息工程股份有限公司，lizhixing，所有权利保留。
 * <p>
 * 项目名：	lovely626
 * 文件名：	TryCatchFinallyTest
 * 模块说明：
 * 修改历史：
 * 2024/7/10 - lizhixing - 创建。
 */
package com.lovely602.design.study.basic;

/**
 * cloneable  是浅拷贝
 *
 * @author lizhixing
 */
public class CloneableTest {

  public static void main(String[] args) {

    User user = getUser();
    System.out.println(user);
  }

  private static User getUser() {

    User user = new User();

    try {
      user.setId(1);
      user.setName("test");
      Role role = new Role();
      role.setId(1);
      role.setName("test");
      user.setRole(role);
      return (User) user.clone();
    } catch (Exception e) {
      return user;
    } finally {
      user.setId(2);
      user.setName("lzx");
      Role role = user.getRole();
      role.setId(2);
      role.setName("lzx");
      user.setRole(role);
    }

  }

  public static class User implements Cloneable {

    private int id;

    private String name;

    private Role role;

    public int getId() {
      return id;
    }

    public void setId(int id) {
      this.id = id;
    }

    public String getName() {
      return name;
    }

    public void setName(String name) {
      this.name = name;
    }

    public Role getRole() {
      return role;
    }

    public void setRole(Role role) {
      this.role = role;
    }

    @Override
    public Object clone() throws CloneNotSupportedException {
      return super.clone();
    }

    @Override
    public String toString() {
      return "User{" +
          "id=" + id +
          ", name='" + name + '\'' +
          ", role=" + role +
          '}';
    }

  }

  public static class Role {
    private int id;

    private String name;

    public int getId() {
      return id;
    }

    public void setId(int id) {
      this.id = id;
    }

    public String getName() {
      return name;
    }

    public void setName(String name) {
      this.name = name;
    }

    @Override
    public String toString() {
      return "Role{" +
          "id=" + id +
          ", name='" + name + '\'';
    }
  }


}
